package com.cn.txx.web.shiro;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.session.mgt.WebSessionKey;

import javax.servlet.ServletRequest;
import java.io.Serializable;

/**
 * @program: ZyPrj
 * @Date: 2018/6/7 21:49
 * @Author: Mr.Tu
 * @Description:
 */
public class CustomSessionManage extends DefaultWebSessionManager {
	@Override
	protected Session retrieveSession(SessionKey sessionKey) throws UnknownSessionException {

		Serializable sessionid = sessionKey.getSessionId();
		ServletRequest request = null;
		if(sessionKey instanceof WebSessionKey){
			request = ((WebSessionKey) sessionKey).getServletRequest();
		}
		if(sessionid != null && request != null){
			Session session = (Session) request.getAttribute(sessionid.toString());
			if(session != null){
				return session;
			}
		};
		Session session = super.retrieveSession(sessionKey);
		if(request != null && session != null){
			request.setAttribute(session.getId().toString(),session);
		}
		return session;
	}
}
